package com.wds.core.entity.base;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户
 * </p>
 *
 * @author dhaigang
 * @since 2024-03-28
 */


@NoArgsConstructor
@Accessors(chain = true)
@TableName("system_user")
@Schema(name = "SystemUser", description = "用户")
@Data
public class SystemUser extends BaseEntity implements UserDetails {

    private static final long serialVersionUID = 1L;

    @TableField(exist = false)
    private User user;

    @Schema(description = "主键")
    @TableId("uid")
    private String uid;

    @Schema(description = "登陆名")
    @TableField("username")
    private String username;

    @JsonIgnore
    @Schema(description = "网络类型")
    @TableField("password")
    private String password;

    @TableField(exist = false)
    private List<SimpleGrantedAuthority> simpleGrantedAuthorityList;

    public SystemUser(User user, List<SimpleGrantedAuthority> simpleGrantedAuthorityList) {
        this.user = user;
        this.simpleGrantedAuthorityList = simpleGrantedAuthorityList;
    }

    @Schema(description = "手机号码")
    @TableField("mobile")
    private String mobile;


    @Schema(description = "启用")
    @TableField("enabled")
    private Boolean enabled;

    @Schema(description = "账户是否过期")
    @TableField("account_non_expired")
    private Boolean accountNonExpired;

    @Schema(description = "账户是否锁定")
    @TableField("account_non_locked")
    private Boolean accountNonLocked;

    @Schema(description = "密码是否过期")
    @TableField("credentials_non_expired")
    private Boolean credentialsNonExpired;


    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return simpleGrantedAuthorityList;
    }

    public List<String> getAuthList() {
        if(null!=simpleGrantedAuthorityList){
            return simpleGrantedAuthorityList.stream().map(x -> x.getAuthority()).collect(Collectors.toList());
        }else return null;


    }
}
